import tuio
import time
from couchdb.client import Server

server=Server('http://blanu.net/')

try:
  db=server.create('nui-map')
except:
  db=server['nui-map']

print 'db: ', db
    
docid='layers'
try:
  db[docid]={'mapName': 'layers', 'data': []}
  doc=db[docid]
  print 'docid:', docid
except:
  doc=db[docid]
  doc['data']=[]
  db[docid]=doc

tracking = tuio.Tracking()
print "loaded profiles:", tracking.profiles.keys()
print "list functions to access tracked objects:", tracking.get_helpers()

oids=set()
nids=set()
try:
    while 1:
        tracking.update()
        for obj in tracking.objects():
            nids.add(obj.id)
#        print nids, oids, nids==oids
        if nids!=oids:
          print 'changed', list(nids)
          print 'doc', doc
          doc['data']=list(nids)
          db[docid]=doc
          print 'doc', doc
        tmp=oids
        oids=nids
        nids=tmp
        nids.clear()
#        time.sleep(1)
except KeyboardInterrupt:
    tracking.stop()
